/**
* @package Scribble
*/
$(document).ready(function() {

	// The order of phases here is important
	window.phases = [
		['restore-point', 'Creating a restore point'],
		['db', 'Creating database backup'],
		['files', 'Archiving files'],
		['plugins', 'Executing plugin backup routines']
	];

	// Start executing the first phase
	execute(window.phases.shift());
});

function execute(phase) {

	// Add phase to the progress list
	$('#phase-progress').append($('<li></li>').html(phase[1]+' ... '));

	// Send server request
	var params = {};
	params['phase-'+phase[0]] = 1;
	$.post('/scribble/upgrade/step-2', params, function(response) {
		if(response.errorCode!=0) {
			$('#phase-progress li:last-child').append($('<span class="error"></span>').html(response.errorMessage));
		}
		else {
			$('#phase-progress li:last-child').append($('<span></span>').html('DONE'));
			if(window.phases.length>0) {
				execute(window.phases.shift());
			}
			else {
				$('#btn-step-3').attr('disabled', '');
			}
		}
	}, 'json');
}